#!/bin/sh -efu
#
# Copyright (C) 2006-2017  Alexey Gladkov <legion@altlinux.org>
# Copyright (C) 2006-2016  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2006  Sergey Vlasov <vsu@altlinux.org>
#
# git commit wrapper for gear.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

. gear-utils-sh-functions
. shell-getopt
. shell-var

show_help()
{
	cat <<EOF
Usage: $PROG [options] [git-commit options] [--] <filepattern>...

Options:

  --spec=FILE         do not look for specfile in repository,
                      use given FILE instead;
  --no-edit           do not edit the message taken from changelog
                      entry or with -m option;
  -e, --edit          edit the message before commit;
  -m, --message=TEXT  use given TEXT for commit message;
  -V, --version       print program version and exit;
  -h, --help          show this text and exit.

Report bugs to http://bugzilla.altlinux.org/

EOF
	exit
}

print_version()
{
	cat <<EOF
$PROG version $PROG_VERSION
Written by Alexey Gladkov <legion@altlinux.org>

Copyright (C) 2006-2017  Alexey Gladkov <legion@altlinux.org>
Copyright (C) 2006-2016  Dmitry V. Levin <ldv@altlinux.org>
Copyright (C) 2006  Sergey Vlasov <vsu@altlinux.org>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
	exit
}

GETOPT_ALLOW_UNKNOWN=1
TEMP=`getopt -n $PROG -o 'e,m:,h,V' -l 'edit,message:,no-edit,spec:,help,version' -- "$@"` || show_usage
eval set -- "$TEMP"

gear_config_option gear_edit_message edit-message yes
gear_config_option gear_require_message_save require-message-save no

commitlog=
specfile=
while :; do
	case "$1" in
		--spec) shift
			specfile="$1"
			[ -z "$commitlog" ] ||
				show_usage "Options --spec and --message are mutually exclusive."
			;;
		--no-edit) gear_edit_message=no
			;;
		-e|--edit)
			gear_edit_message=yes
			;;
		-m|--message) shift
			commitlog="$1"
			[ -z "$specfile" ] ||
				show_usage "Options --spec and --message are mutually exclusive."
			;;
		-h|--help) show_help
			;;
		-V|--version) print_version
			;;
		--) shift; break
			;;
	esac
	shift
done

GEAR_COMMIT=1
GEAR_COMMIT_AMEND=

args=
while [ $# -gt 0 ]; do
	[ "$1" != '--amend' ] ||
		GEAR_COMMIT_AMEND=1

	args="$args \"$(quote_shell "$1")\""
	shift
done

# Change to toplevel directory
chdir_to_toplevel

guess_specfile

[ -s "$specfile" ] ||
	fatal "$specfile: file not available or empty"

install_cleanup_handler cleanup_workdir
workdir="$(mktemp -dt "$PROG.XXXXXXXXXX")"

if [ -z "$commitlog" ]; then
	vr="$(sed -ne '/^[[:space:]]*%changelog[[:space:]]*$/{n; s/\* .* \([^[:space:]]\+\)$/\1/p}' "$specfile")"
	ch="$(sed -ne '0,/^[[:space:]]*%changelog[[:space:]]*$/d;2,/^* /{/^* /!p}' "$specfile")"
	[ -z "$vr$ch" ] ||
		commitlog="$(printf '%s\n\n%s\n' "$vr" "$ch" |sed s/%%/%/g)"
	unset ch vr specfile
fi

{
	! shell_var_is_yes "$gear_require_message_save" ||
		printf '%s\n\n' '<gear-commit message template, please remove this line and save to commit>'
	printf '%s\n' "$commitlog"
} > "$workdir/commit-msg"

export GEAR_COMMIT GEAR_COMMIT_AMEND

git_edit=
git_msgtype='-F'

! shell_var_is_yes "$gear_edit_message" || git_edit='--edit'
! shell_var_is_yes "$gear_require_message_save" || git_msgtype='-t'

eval git commit $git_msgtype "$workdir/commit-msg" $git_edit $args
